package edu.spec.wechat.controller;

import edu.spec.wechat.model.WechatFianlValue;
import edu.spec.wechat.model.WechatMenu;
import edu.spec.wechat.web.WechatContext;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MadFrog on 2017/5/29.
 */
@Controller
public class MenuController {

    @RequestMapping(value = "/wechat/createMenu", method = RequestMethod.GET)
    public String createMenu(){
        String mappingUrl = "http://xiong.ittun.com";
        String ctx = "/sms";
        //活动
        //       活动报名(url)
        //       活动签到(扫描事件)
        //绑定学号(url)

        // wechat菜单总list
        List<WechatMenu> wechatMenuList = new ArrayList<WechatMenu>();

        WechatMenu level1Menu1 = new WechatMenu();
        level1Menu1.setName("系部");
        WechatMenu level1Menu11 = new WechatMenu();
        level1Menu11.setName("简介");
        level1Menu11.setType("view");
        level1Menu11.setKey("event");
        level1Menu11.setUrl(mappingUrl + ctx + "/yyjsx/introduction");
        WechatMenu level1Menu12 = new WechatMenu();
        level1Menu12.setName("物联网示范");
        level1Menu12.setType("view");
        level1Menu12.setKey("event");
        level1Menu12.setUrl(mappingUrl + ctx + "/yyjsx/iot");
        List<WechatMenu> level1List1 = new ArrayList<WechatMenu>();
        level1List1.add(level1Menu11);
        level1List1.add(level1Menu12);


        WechatMenu level1Menu2 = new WechatMenu();
        level1Menu2.setName("学生");

        WechatMenu level1Menu21 = new WechatMenu();
        level1Menu21.setName("学工系统");
        level1Menu21.setType("view");
        level1Menu21.setKey("event");
        level1Menu21.setUrl(mappingUrl + ctx + "/student/index");
        WechatMenu level1Menu22 = new WechatMenu();
        level1Menu22.setName("活动签到");
        level1Menu22.setType("scancode_waitmsg");
        level1Menu22.setKey("event");
        List<WechatMenu> level1List2 = new ArrayList<WechatMenu>();
        level1List2.add(level1Menu21);
        level1List2.add(level1Menu22);

        WechatMenu level1Menu3 = new WechatMenu();
        level1Menu3.setName("教工");
        level1Menu3.setType("view");
        level1Menu3.setUrl(mappingUrl + ctx + "/teacher/index");

        // wechatMenuList
        level1Menu1.setSub_button(level1List1);
        level1Menu2.setSub_button(level1List2);
        wechatMenuList.add(level1Menu1);
        wechatMenuList.add(level1Menu2);
        wechatMenuList.add(level1Menu3);

        // 最终封装
        Map<String, List<WechatMenu>> menuMap = new HashMap<String, List<WechatMenu>>();
        menuMap.put("button", wechatMenuList);
        String jsonStr = JsonUtil.getInstance().obj2json(menuMap);
        System.out.println(jsonStr);

        try {
            String url = WechatFianlValue.MENU_CREATE.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            System.out.println(url);

            post.addHeader("ContentType", "application/json");
            StringEntity entity = new StringEntity(jsonStr, ContentType.create("application/json", "utf-8"));
            post.setEntity(entity);
            CloseableHttpResponse resp = client.execute(post);
            int sc = resp.getStatusLine().getStatusCode();
            if(sc>=200&&sc<300){
                System.out.println(EntityUtils.toString(resp.getEntity()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return "student/index";
        }
    }

    @RequestMapping(value = "/wechat/getMenu", method = RequestMethod.GET)
    public void getMenu(){
        try {
            String url = WechatFianlValue.MENU_GET.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            System.out.println(url);

            CloseableHttpResponse resp = client.execute(get);
            int sc = resp.getStatusLine().getStatusCode();
            if(sc>=200&&sc<300){
                System.out.println(EntityUtils.toString(resp.getEntity()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/wechat/deleteMenu", method = RequestMethod.GET)
    public void deleteMenu(){
        try {
            String url = WechatFianlValue.MENU_DELETE.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            System.out.println(url);

            CloseableHttpResponse resp = client.execute(get);
            int sc = resp.getStatusLine().getStatusCode();
            if(sc>=200&&sc<300){
                System.out.println(EntityUtils.toString(resp.getEntity()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
